home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0386.arc / AIEYE1.LTG < prev    next >
Text File  |  1986-03-03  |  2KB  |  51 lines

  1.  
  2.  
  3.                             Listing 1
  4.                          Match in Prolog
  5.  
  6.  
  7.             % Match(P,E) is a predicate that instantiates its arguments
  8.             % such that P is a pattern template matching the pattern
  9.             % example E.  A pattern template is a list or nested list
  10.             % containing atomics and pattern operators.  A pattern example
  11.             % is a list or nested list containing atomics only (can you
  12.             % see why we exclude pattern operators in the pattern example?).
  13.             %
  14.             % Pattern operators in this version include ? (match one element)
  15.             % and + (match one or more elements).
  16.  
  17.       [1]   match([],[]).                    % Two nil lists match
  18.  
  19.       [2]   match([EH | PT], [EH | ET]) :-   % Lists starting with same thing
  20.       [3]         match(PT,ET).              % match if their tails match
  21.  
  22.       [4]   match([+ | PT], E) :-            % A + matches 1 or more elements,
  23.       [5]         append([EH | _],ET,E),     % so instantiate non-nil tail of E
  24.       [6]         match(PT,ET).              % & succeed if it matches tail of P
  25.  
  26.       [7]   match([? | PT], [EH | ET]) :-    % ? matches one element, so succeed
  27.       [8]         match(PT, ET).             % if tails of P & E match
  28.  
  29.       [9]   match([PH | PT], [EH | ET]) :-   % Nested lists match if
  30.       [10]        match(PH,EH),              % their heads match
  31.       [11]        match(PT,ET).              % and their tails match
  32.  
  33.             % Induce instantiates its arguments such that P is a pattern
  34.             % template matching both example lists E1 and E2.
  35.  
  36.       [12]  induce(E1,E2,P) :- match(P,E1), match(P,E2).
  37.  
  38.             % Classify instantiates its arguments such that P is a member of
  39.             % the list of pattern templates PL and P matches the example
  40.             % list E.
  41.  
  42.       [13]  classify(E,PL,P) :- member(P,PL), match(P,E).
  43.  
  44.             % Append(L1,L2,L3) instantiates its arguments such that
  45.             % the list L3 is the result of concatenating lists L1 and L2.è
  46.       [14]  append([],L,L).                  % Appending a list to nil is no-op
  47.       [15]  append([X|L1],L2,[X|L3]) :-      % Appending 2nd arg to 1st means
  48.                    append(L1,L2,L3).         % append 2nd arg to 1st's TAIL then
  49.                                              % add head of 1st, X, to result, L3
  50.  
  51.